#e
#Title[xugbN{[ -Lunatic-v]
#Text[]
#Player[FREE]
#Image[.\img\trickLuna.png]
#ScriptVersion[2]

script_enemy_main {
	let name	= "xugbN{[v";
    	let csd         = GetCurrentScriptDirectory;
	let imgRumia	= csd ~ "img\ExRumia.png";
    	let shotImage   = csd ~ "data_usershot.txt";
	let WAV1	= csd ~ "SE\shot1.wav";
	let WAV2	= csd ~ "SE\enemy_vanish[GtFNga].wav";
	let wIni	= 120;
	let base	= rand(0,360);
	
	///////////////////////////////////////////////////////////

	@Initialize{
        CutIn(YOUMU, name, "", 0, 0, 0, 0);
	LoadGraphic(imgRumia);
	    
        SetMovePosition02(GetCenterX(), GetClipMinY + 120, wIni);
        SetLife(5000);
        SetTimer(81);
	SetScore(500000);
        SetDamageRate(100, 20);
	SetInvincibility( 260 );
        LoadUserShotData(shotImage);

	TMain();

	}
		
	@MainLoop{
	    SetCollisionA(GetX, GetY, 32);
	    SetCollisionB(GetX, GetY, 16);

	    yield;
	}
	
	@DrawLoop{
	    SetTexture(imgRumia);
	    DrawGraphic(GetX(), GetY());
	    if(GetSpeedX()==0){SetGraphicRect(64,1,127,64);}
	    else if(GetSpeedX()>0){SetGraphicRect(192,1,255,64);}
	    else if(GetSpeedX()<0){SetGraphicRect(128,1,191,64);}
	}
		
	@Finalize{
	    DeleteGraphic(imgRumia);
	}


    task TMain{

	yield;
	loop(wIni){ yield; }

	ascent(let i in 0..10){
	PlaySE(WAV1);
	let color	= rand_int(33,40);
	trick(GetX, GetY, 5,
                 base+i*360/10, color, 30, 6);
	loop(3){ yield; }
	}
	loop(3){
	loop(60){ yield; }
	move;
	}
	ascent(let i in 0..10){
	PlaySE(WAV1);
	let color	= rand_int(33,40);
	trick(GetX, GetY, 5,
                 base-i*360/10, color, 30, 10);
	loop(3){ yield; }
	}

	loop{
	move;
	loop(80){ yield; }
	}

    }

	///////////////////////////////////////////////////////////


   task trick(let x, let y, speed,
                 let angle, graphic, let delay, let count) {
        
        //IuWFNge̍쐬
        let obj 	= Obj_Create(OBJ_SHOT);
        
        //p[^̐ݒ
        Obj_SetPosition(obj, x, y);
        Obj_SetSpeed(obj, speed);
        Obj_SetAngle(obj, angle);
        ObjShot_SetGraphic(obj, graphic);
        ObjShot_SetDelay(obj, delay);
	Obj_SetAutoDelete(obj, false);
	ObjShot_SetBombResist(obj, true);
        

        while(!Obj_BeDeleted(obj)) {
	    if(count==10||count==8||count==6||count==4||count==2){
            //[
            if(Obj_GetX(obj)<GetClipMinX){
                Obj_SetX(obj, GetClipMaxX);
		count--;
            }
            //E[
            if(Obj_GetX(obj)>GetClipMaxX){
                Obj_SetX(obj, GetClipMinX);
		count--;
            }
            //[
            if(Obj_GetY(obj)<GetClipMinY){
                Obj_SetY(obj, GetClipMaxY);
		count--;
            }
            //[
            if(Obj_GetY(obj)>GetClipMaxY){
                Obj_SetY(obj, GetClipMinY);
		count--;
            }
	}
	    if(count==9||count==7||count==5||count==3||count==1){
            //[
            if(Obj_GetX(obj)<GetClipMinX){
                Obj_SetX(obj, GetClipMinX*2 - Obj_GetX(obj));
                Obj_SetAngle(obj, 180 - Obj_GetAngle(obj));
		count--;
            }
            //E[
            if(Obj_GetX(obj)>GetClipMaxX){
                Obj_SetX(obj, GetClipMaxX*2 - Obj_GetX(obj));
                Obj_SetAngle(obj, 180 - Obj_GetAngle(obj));
		count--;
            }
            //[
            if(Obj_GetY(obj)<GetClipMinY){
                Obj_SetY(obj, GetClipMinY*2 - Obj_GetY(obj));
                Obj_SetAngle(obj, 360 - Obj_GetAngle(obj));
		count--;
            }
            //[
            if(Obj_GetY(obj)>GetClipMaxY){
                Obj_SetY(obj, GetClipMaxY*2 - Obj_GetY(obj));
                Obj_SetAngle(obj, 360 - Obj_GetAngle(obj));
		count--;
            }
	}
            
        if(count<=0){
	loop(30){ yield; }
	ascent(let i in 0..16){
	CreateShot01(Obj_GetX(obj),Obj_GetY(obj),rand(3,5),rand(0,360),graphic+120,20);
	}
	PlaySE(WAV2);
	Obj_Delete(obj);
	let color	= rand_int(33,40);
	PlaySE(WAV1);
	trick(GetX, GetY, 5,
                 rand(0,360), color, 30, 6);
	}
        
            yield;
        }

    }


    // ړ
    sub move {
        let wMove = 50;

        moveToPlayer(rand(40, 80), rand(-40, 40), wMove,
                     GetClipMinX + 48, GetClipMinY +  32,
                     GetClipMaxX - 48, GetClipMinY + 128);
	wait(wMove);
    }

    // ȂׂvC[̕Ɉړ
    //   xMove  : x ̈ړʁi̐j
    //   yAdd   : y ̈ړ
    //   frame  : ړɗvt[
    //   left   : ȉA͈
    //   top    :
    //   right  :
    //   bottom :
    function moveToPlayer(xMove, yAdd, frame, left, top, right, bottom) {
        let x;
        let y;

        if(GetPlayerX < GetX) {
            // vC[EɓG΁AG͍ɓ܂B
            x = GetX - xMove;

            // AAG̈̍[ɂ悤ȂAEɓ܂B
            if(x < left) {
                x = GetX + xMove;
            }
        } else {
            // Ȃ΁AG͉Eɓ܂B
            x = GetX + xMove;

            // AAG̈̉E[Eɂ悤ȂAɓ܂B
            if(right < x) {
                x = GetX - xMove;
            }
        }

        // ̈̊Oɍsꍇ́A[Ŏ~߂܂B
        y = GetY + yAdd;
        if(y < top) {
            y = top;
        } else if(bottom < y) {
            y = bottom;
        }

        SetMovePosition02(x, y, frame);
    }
	
    // w t[҂
    function wait(w) {
        loop(w) { yield; }
    }
}